package com.rimi.bbs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rimi.bbs.bean.Card;
import com.rimi.bbs.bean.Replies;
import com.rimi.bbs.mapper.CardMapper;
import com.rimi.bbs.mapper.RepliesMapper;
import com.rimi.bbs.mapper.UserMapper;
import com.rimi.bbs.service.RepliesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jerry
 * @since 2019-02-24
 */
@Service
public class RepliesServiceImpl extends ServiceImpl<RepliesMapper, Replies> implements RepliesService {


    @Autowired
    private RepliesMapper repliesMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CardMapper cardMapper;

    public IPage<Replies> getRepliesList(Integer cardId,Integer nowPage, Integer pageSize){
        IPage<Replies> repliesIPage = repliesMapper.selectPage(new Page<Replies>(nowPage,pageSize), new QueryWrapper<Replies>()
                .eq("card_id",cardId)
                .orderByDesc("replies_status"));
        for (Replies record : repliesIPage.getRecords()) {
            record.setUser(userMapper.selectById(record.getUserId()));
        }
        return repliesIPage;
    }

    @Override
    public List<Replies> getRepliesByUserId(Integer userId) {
        List<Replies> repliesList = repliesMapper.selectList(new QueryWrapper<Replies>()
                .eq("user_id", userId)
                .orderByDesc("replies_id", "replies_time"));
        //查询回复对应的标题
        for (Replies replies : repliesList) {
            Card card = cardMapper.selectById(replies.getCardId());
            replies.setCardTitle(card.getCardTitle());
        }
        return repliesList;
    }


}
